package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileFilter;

import system.ButtonActionListener;
import system.ImageFactory;
import system.PanelFactory;
import system.SystemProperties;

/**
 * @author Kevin Peters
 */
public class MainPanel extends JFrame implements ChangeListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8438576029794021570L;

	private static Dimension currentDesktop;

	private static JColorChooser colorChooser;

	private static BufferedImage image;

	private static MaskGrid maskGrid;

	private static ControlPanel controlPanel;

	private SystemProperties systemProperties;
	
	private ButtonActionListener buttonActionListener;
	
	private static Color defaultColor = Color.BLACK;

	/**
	 * Initializes needed elements for the gui
	 */
	private void initGui() {
		setLayout(new FlowLayout());
		setVisible(true);
		currentDesktop = Toolkit.getDefaultToolkit().getScreenSize();
		setPreferredSize(new Dimension(currentDesktop.width,
				currentDesktop.height));
		setResizable(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		maskGrid = new MaskGrid();
		buttonActionListener = new ButtonActionListener(this);
		controlPanel = new ControlPanel(buttonActionListener);

		colorChooser = new JColorChooser(defaultColor);
		colorChooser.getSelectionModel().addChangeListener(this);
		colorChooser.setBorder(BorderFactory
				.createTitledBorder(SystemProperties.COLORCHOOSERTITLE));
	}

	/**
	 * Loads the image from the given path to the grid panel
	 * 
	 * @param imagePath
	 */
	public void loadImage(String imagePath, boolean extern) {
		image = ImageFactory.loadImage(imagePath, extern);
		if(image == null){
			image = ImageFactory.loadImage(SystemProperties.RESOURCEPATH
					+ SystemProperties.DEFAULTPICTURENAME, false);
		}
		PanelFactory.addPanels(this, maskGrid, image);
		maskGrid.documentAllPanels(systemProperties);
		
	}

	public MainPanel(SystemProperties systemProperties) {
		super(SystemProperties.APPTITLE);
		this.systemProperties = systemProperties;
		initGui();
		loadImage(SystemProperties.RESOURCEPATH
				+ SystemProperties.DEFAULTPICTURENAME, false);
		add(maskGrid);
		maskGrid.noticeAllPanels(defaultColor);
		add(colorChooser);
		add(controlPanel);
		pack();
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				SystemProperties systemProperties = new SystemProperties();
				systemProperties.readPropertiesFile();
				systemProperties.convertDataIntoTypes();
				systemProperties.readLocalTranslations();
				new MainPanel(systemProperties);
			}

		});
	}

	public void setMaskGrid(MaskGrid maskGrid) {
		MainPanel.maskGrid = maskGrid;
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		maskGrid.noticeAllPanels(colorChooser.getColor());
	}

	/**
	 * Removes all gui components and loads the image from the given path to the
	 * grid panel
	 * 
	 * @param imagePath
	 */
	public void reloadImage(String imagePath, boolean extern) {

		remove(maskGrid);
		remove(controlPanel);
		remove(colorChooser);
		MaskGrid maskGrid = new MaskGrid();
		setMaskGrid(maskGrid);
		loadImage(imagePath, extern);
		add(maskGrid);
		add(colorChooser);
		add(controlPanel);
		pack();
	}

	/**
	 * Offers a file browser to choose a custom image to edit
	 * 
	 * @return Path to the selected file
	 */
	public String chooseFilePath(boolean save) {
		JFileChooser chooser = new JFileChooser(new File(SystemProperties.DEFAULT_FILE_PATH));
		
		chooser.setFileFilter(new FileFilter() {
            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith("."+SystemProperties.FILE_EXTENSION) || f.isDirectory();
            }
            public String getDescription() {
                return SystemProperties.FILE_PATH_DESCRIPTION;
            }
        });
		if (save) {
			chooser.showSaveDialog(this);
		} else {
			chooser.showOpenDialog(this);
		}

		File file = chooser.getSelectedFile();
		if (file != null){
			return file.getAbsolutePath();
		} else{
			return null;
		}

		
	}

	public MaskGrid getMaskGrid() {
		return maskGrid;
	}

	public JColorChooser getColorChooser() {
		return colorChooser;
	}

}